package com.rtline.business.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtline.business.common.Result;
import com.rtline.business.convert.UserConvert;
import com.rtline.business.model.UserEntity;
import com.rtline.business.service.UserService;
import com.rtline.business.vo.UserVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>UserController .
 *
 * <p>change history:
 *
 * <pre>
 * date         defect       person          comments
 * --------------------------------------------------------------------------------
 * 2025/5/17    ********     Tao Ren    create file.
 * </pre>
 *
 * @author Tao Ren
 * @date 2025/5/17 21:13
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserConvert userConvert;

    @GetMapping("/{id}")
    public UserEntity getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PostMapping("/add")
    public UserEntity insertUser(@RequestBody UserEntity user) {
        userService.insertUser(user);
        return user;
    }

    @GetMapping("/getAll")
    public List<UserEntity> getAllUsers() {
        return userService.list();
    }

    @GetMapping("/getById/{id}")
    public UserVo getUserByIdPlus(@PathVariable Long id) {
        UserEntity userEntity = userService.getById(id);
        return userConvert.toDTO(userEntity);
    }

    @PostMapping("/createUser")
    public UserEntity createUser(@RequestBody UserEntity user) {
        userService.save(user);
        return user;
    }

    @PutMapping("/update/{id}")
    public UserEntity updateUser(@PathVariable Long id, @RequestBody UserEntity user) {
        user.setId(id);
        userService.updateById(user);
        return user;
    }

    @DeleteMapping("/delete/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.removeById(id);
    }

    /**
     * 分页查询用户列表
     *
     * @param current 当前页码
     * @param size    每页显示条数
     * @param params  查询参数（可选）
     * @return 分页结果
     */
    @GetMapping("/page")
    public IPage<UserEntity> getUsersByPage(
            @RequestParam(value = "current", defaultValue = "1") int current,
            @RequestParam(value = "size", defaultValue = "2") int size,
            @RequestParam(value = "params", required = false) Map<String, Object> params) {
        Page<UserEntity> page = new Page<>(current, size);
        return userService.pageList(page, params);
    }

    @PostMapping("/operate")
    public Result operate(@RequestBody Map<String, Object> params) {

        return userService.operate(params);
    }

    @GetMapping("/data/{userId}")
    public CompletableFuture<String> getUserData(@PathVariable String userId) {
        return userService.getUserData(userId);
    }

    @GetMapping("/test/transactional")
    public void testTransactional() {
        userService.testTransactional2();
    }
}
